﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvidenceManagement.PresentationModel.Security;
using EvidenceManagement.ViewModel.Common;
using EvidenceManagement.ViewModel.Security;

namespace EvidenceManagement.PresentationModel.Common
{
    public class MasterPresenter : IMasterPresenter
    {
        readonly IMasterView masterView = null;
        List<IView> childViews;

        public MasterPresenter(IMasterView view)
        {
            masterView = view;
            childViews = new List<IView>();
        }

        public void LoadLogin(IView logOnView)
        {
            if (!childViews.Contains(logOnView))
                childViews.Add(logOnView);

            if ((logOnView is ILogOnView))
                (logOnView as ILogOnView).OnLogOnPassed += new EventHandler(OnLogOnPassed);

            MasterView.LoadChildView(logOnView);
        }

        public void OnLogOnPassed(object sender, EventArgs e)
        {
            try
            {
                (sender as IView).CloseView();
            }
            finally
            {

                (sender as ILogOnView).OnLogOnPassed -= new EventHandler(this.OnLogOnPassed);
            }
        }

        #region IPresenter Members

        public void OnLoad()
        {

        }

        public IMasterView MasterView
        {
            get { return masterView; }
        }

        public List<IView> ChildViews
        {
            get { return childViews; }
        }

        public void OnClose()
        {
            foreach (IView view in ChildViews)
            {
                view.CloseView();
            }
        }

        #endregion
    }
}
